package com.yami.shop.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.pt.YxCtjl;
import com.yami.shop.common.util.PageAdapter;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.dao.YxCtjlMapper;
import com.yami.shop.service.IYxCtjlService;
import com.yami.shop.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YxCtjlServiceImpl extends ServiceImpl<YxCtjlMapper,YxCtjl> implements IYxCtjlService {
    @Autowired
    private YxCtjlMapper yxCtjlMapper;
    @Autowired
    private UserService userService;

    /**
     * 查询参团记录信息
     *
     * @param ctjlid 参团记录ID
     * @return 参团记录信息
     */
    @Override
    public YxCtjl selectYxCtjlById(Long ctjlid)
    {
        return yxCtjlMapper.selectYxCtjlById(ctjlid);
    }

    /**
     * 查询参团记录列表
     *
     * @param yxCtjl 参团记录信息
     * @return 参团记录集合
     */
    @Override
    public IPage<YxCtjl> selectYxCtjlList(Page<YxCtjl> page, YxCtjl yxCtjl)
    {
        //记录
        List<YxCtjl> yxCtjlList =  yxCtjlMapper.selectYxCtjlList(new PageAdapter(page),yxCtjl);
        if(CollectionUtils.isNotEmpty(yxCtjlList)){
            for(YxCtjl yxCtjl1 : yxCtjlList){
                User user = userService.getUserByUserId(yxCtjl1.getUserId());
                if (user != null){
                    yxCtjl1.setWxNickname(user.getNickName());
                    yxCtjl1.setWxAvatarurl(user.getPic());
                }
            }
        }
        page.setRecords(yxCtjlList);
        page.setSize(yxCtjlMapper.getjlNumber(yxCtjl));
        return page;
    }

    /**
     * 新增参团记录
     *
     * @param yxCtjl 参团记录信息
     * @return 结果
     */
    @Override
    public int insertYxCtjl(YxCtjl yxCtjl)
    {
        return yxCtjlMapper.insertYxCtjl(yxCtjl);
    }

    /**
     * 修改参团记录
     *
     * @param yxCtjl 参团记录信息
     * @return 结果
     */
    @Override
    public int updateYxCtjl(YxCtjl yxCtjl)
    {
        return yxCtjlMapper.updateYxCtjl(yxCtjl);
    }

    /**
     * 删除参团记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteYxCtjlByIds(String ids)
    {
        return yxCtjlMapper.deleteYxCtjlByIds(Convert.toStrArray(ids));
    }

    @Override
    public List<YxCtjl> selectYxCtjlListPtglid(Long ptglid){
        return yxCtjlMapper.selectYxCtjlListPtglid(ptglid);
    }
    @Override
    public int updateYxCtjlStatusByPtglid( String status, Long ptglid){
        return yxCtjlMapper.updateYxCtjlStatusByPtglid(status,ptglid);
    }

    @Override
    public YxCtjl getYxCtjl(String userId,Long prodId) {
        return yxCtjlMapper.getYxCtjl(userId,prodId);
    }
}
